<template>
  <el-form
    ref="userForm"
    :model="userInfo"
    label-width="120px"
    style="margin-left: 120px; margin-top: 30px"
  >
    <el-form-item label="姓名:">
      <el-input v-model="userInfo.username" style="width: 300px" />
    </el-form-item>
    <el-form-item label="密码:">
      <el-input
        v-model="userInfo.password"
        type="password"
        style="width: 300px"
      />
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="hSubmit">更新</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { userDetailById } from '@/api/user'
import { saveUserDetailById } from '@/api/employees'

export default {
  data() {
    return {
      userInfo: {
        username: '',
        password: ''
      }
    }
  },
  created() {
    this.loadUserDetailById()
  },
  methods: {
    // 获取用某个用户的信息
    async loadUserDetailById() {
      const { data: res } = await userDetailById(this.$route.query.id)
      // console.log(res)
      this.userInfo = res
    },
    // 点击更新
    hSubmit() {
      this.doEdit()
    },
    // 更新信息请求
    async doEdit() {
      try {
        // 请求
        await saveUserDetailById(this.userInfo)
        // 提示
        this.$message.success('修改成功')
        // 重新回填
        this.loadUserDetailById()
      } catch (error) {
        this.$message.error('修改失败')
      }
    }
  }
}
</script>
